// 获取餐点列表信息
var urlList = server_host + '/foodview/getFoodList.do';
// 包房列表信息
var urltableList = server_host + '/foodview/getTableNameList.do';

var urlGetUserOrderDetail = server_host + '/userweb/getUserOrderDetail.do';
// 获取shopName 20151104 Pm加需求单独打开此页面
var urlGetShopName = server_host + '/mainview/getShopName.do';

var fr = getParameterByName('fr');

var params = getParameterByName('params');

// 包厢订单列表点餐
var orderNum;
if (fr == 'order') {
	// shopId = getParameterByName('shopId');
	// shopName = decodeURIComponent(getParameterByName('shopName'));
	orderNum = decodeURIComponent(getParameterByName('orderNum'));
}

// 商户预订详细页点餐
var shopId = sessionStorage.getItem('shopId');
var shopName = sessionStorage.getItem('shopName');

var appModule = angular.module('wxFood', []);
appModule.controller('FoodListCtrl', ['$scope', '$http', function($scope, $http) {

	$scope.fr = fr;

	//如果是从下一个页面返回，会带有已经选择的餐点信息
	$scope.orderedMeal = params != '' ? eval('(' + decodeURIComponent(atob(params)) + ')') : '';

	$scope.shopName = shopName;

	$scope.total = 0;
	$scope.amount = 0;

	
	// 单独进店shopid用地址栏的、否则为上页session的shopId!!!20151104 PM改需求kao~
	// $scope.shopId = getParameterByName('shopId')?getParameterByName('shopId'):shopId;
	if(getParameterByName('shopId')){
		$scope.shopId = getParameterByName('shopId');
		sessionStorage.setItem('shopId',$scope.shopId);

	}else{
		$scope.shopId = shopId;
	}

	$scope.params = {
		ktvOrderNum: -1,
		userPhone: sessionStorage.getItem('userPhone'),
		shopId: sessionStorage.getItem('shopId'),
		userMsg: '',
		tableId: -1,
		tableName: '',
		foodList: []
	}

	$scope.getShopName = function(){
		$http.get(urlGetShopName + '?shopId=' + $scope.shopId).success(function(data, status, headers, config) {
			console.log(data);
			$scope.shopName = data.shopName;
		})
	}
	$scope.loadList = function() {
		$http.get(urlList + '?shopId=' + $scope.shopId).success(function(data, status, headers, config) {
			console.log(data);
			$scope.foodDetails = data;
			if ($scope.orderedMeal != '') {
				//如果是从下一个页面返回，恢复已选择的餐点
				// angular.forEach($scope.orderMeal.foodList, function(item) {
				// 	angular.forEach($scope.foodDetails, function(foodList) {
				// 		angular.forEach(foodList.foodList, function(food) {

				// 		});
				// 	});
				// });
				for (var i = 0; i < $scope.foodDetails.length; i++) {
					var food = $scope.foodDetails[i];
					for (var j = 0; j < food.foodList.length; j++) {
						var foodObj = food.foodList[j];
						var foodNumber = 0;
						angular.forEach($scope.orderedMeal.foodList, function(item) {
							if (item.foodId == foodObj.foodId) {
								foodNumber = item.foodNumber;
							}
						});
						foodObj.foodNumber = foodNumber;
					}
				}
				$scope.calcTotal();
				$scope.calcAmount();
			} else {
				//如果是单独点餐流程，初始化餐点数量
				for (var i = 0; i < $scope.foodDetails.length; i++) {
					var food = $scope.foodDetails[i];
					for (var j = 0; j < food.foodList.length; j++) {
						food.foodList[j].foodNumber = 0;
					}
				}
			}
			//当前选中餐点类型
			for (var i = 0; i < $scope.foodDetails.length; i++) {
				$scope.foodDetails[i].isShow = false;
			}
			$scope.foodDetails[0].isShow = true;
			// $scope.cateId = foodDetails.data;
			// $scope.
		});
	};

	$scope.getUserOrderDetail = function() {
		$http.get(urlGetUserOrderDetail + 　'?orderNum=' + orderNum).success(function(data, status, headers, config) {
			console.log(data);
			$scope.UserOrderDetail = data;
			$scope.params.tableId = data.data.tableId;
			$scope.params.ktvOrderNum = data.data.orderNum;
			$scope.params.tableName = data.data.tableName;
			// $scope.tableId = data;
		})
	}

	$scope.switchFood = function(_i) {
		for (var i = 0; i < $scope.foodDetails.length; i++) {
			$scope.foodDetails[i].isShow = false;
		}
		$scope.foodDetails[_i].isShow = true;
	}

	$scope.foodDetail = function(cateIndex, foodId) {
		// 根据属性、属性值找到当前对象
		$scope.detailObj = findElement($scope.foodDetails[cateIndex].foodList, 'foodId', foodId);
		$('#myModal').modal('show');
	}

	// 按钮
	$scope.decrease = function(cateIndex, foodId, $event) {
		// 阻止冒泡
		$event.stopPropagation();
		// 要执行的代码
		findElement($scope.foodDetails[cateIndex].foodList, 'foodId', foodId).foodNumber--;

		$scope.calcTotal();
		$scope.calcAmount();
	}

	$scope.increase = function(cateIndex, foodId, e) {
		e.stopPropagation();
		findElement($scope.foodDetails[cateIndex].foodList, 'foodId', foodId).foodNumber++;

		$scope.calcTotal();
		$scope.calcAmount();
	}

	// 计算价格
	$scope.calcTotal = function() {
		$scope.total = 0;
		for (var i = 0; i < $scope.foodDetails.length; i++) {
			var food = $scope.foodDetails[i];
			for (var j = 0; j < food.foodList.length; j++) {
				$scope.total = (parseFloat($scope.total) + food.foodList[j].foodNumber * parseFloat(food.foodList[j].price)).toFixed(2);
			}
		}
	}

	// 计算餐点份数 
	$scope.calcAmount = function() {
		$scope.amount = 0;
		for (var i = 0; i < $scope.foodDetails.length; i++) {
			var food = $scope.foodDetails[i];
			for (var j = 0; j < food.foodList.length; j++) {
				$scope.amount += food.foodList[j].foodNumber;
			}
		}
	}

	// 包厢选取
	$scope.tableList = function() {
		$http.get(urltableList + '?shopId=' + $scope.shopId).success(function(data, status, headers, config) {
			console.log(data);
			$scope.tableDetail = data;
			$scope.tableDetail.unshift({
				tableId: -1,
				tableName: "选择包厢"
			});
		});
	};

	// 新建数组
	$scope.genSelected = function() {
		if ($scope.params.tableId == -1 && $scope.fr != 'order') {
			alert('请选择包厢！');
			return false;
		} else {
			$scope.params.foodList = [];
			for (var i = 0; i < $scope.foodDetails.length; i++) {

				var food = $scope.foodDetails[i];
				for (var j = 0; j < food.foodList.length; j++) {

					var foodObj = food.foodList[j];
					if (foodObj.foodNumber > 0) {
						$scope.params.foodList.push({

							foodId: foodObj.foodId,
							foodNumber: foodObj.foodNumber,
							foodName: foodObj.foodName,
							unit: foodObj.unName,
							foodPrice: foodObj.price

						});
					}
				}
			}

			if ($scope.params.foodList.length == 0) {
				alert('请至少选择一个餐点！');
				return false;
			} else
				return true;
		}
	}

	//点击购物车
	$scope.shoppingCart = function() {
		if ($scope.genSelected())
			location.href = 'shopping-cart.html?params=' + btoa(encodeURIComponent(JSON.stringify($scope.params)));
	}

	// 点击提交
	$scope.foodIds = '';
	$scope.submit = function() {
		if ($scope.genSelected()) {

			for(var i=0; i < $scope.params.foodList.length; i++){
				if(i == 0){
					$scope.foodIds = $scope.params.foodList[0].foodId;
				}else{
					$scope.foodIds += ',' + $scope.params.foodList[i].foodId;
				}
			}

			console.log($scope.foodIds);
			location.href = 'order-food-confirm.html?params=' + btoa(encodeURIComponent(JSON.stringify($scope.params))) + '&fr=foodinfo' + '&foodIds=' + $scope.foodIds;
		}
	}

	// 模态框的点击详情
	$("#slide-btn").click(function() {

		if ($(".info").is(":hidden")) {
			$("#slide-btn").addClass("link");
		} else {
			$("#slide-btn").removeClass("link");
		};

		$(".info").slideToggle("fast");

		// 监听模态框是否关闭
		// 是：详细信息隐藏
		$('.close').click(function() {
			$('.info').css('display', 'none');
		});
	});


	$scope.loadList();
	$scope.tableList();
	$scope.getShopName();

	//监听『选择包厢号码』select，tableId变化
	$scope.$watch(function() {
		return $scope.params.tableId;
	}, function(a, b) {
		if (typeof($scope.tableDetail) != 'undefined')
			$scope.params.tableName = findElement($scope.tableDetail, 'tableId', a).tableName;
	});

	if (fr == 'order')
		$scope.getUserOrderDetail();

	//如果是从下一个页面返回，会带有已经选择的餐点信息
	if ($scope.orderedMeal != '') {
		//恢复已选择的包厢号
		$scope.params.tableId = $scope.orderedMeal.tableId;
		//恢复已选择的餐点
		// angular.forEach($scope.orderMeal.foodList, function(item){
		// 	angular.forEach($scope.foodDetails, function(foodList){
		// 		angular.forEach(foodList.foodList, function(food){

		// 		});
		// 	});
		// });
	}

}]);